﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SubSonic;

namespace AppBox.admin
{
    public partial class menu : PageBase
    {
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnNew.OnClientClick = Window1.GetShowReference("~/admin/menu_new.aspx", "新增菜单");


            BindGrid();
        }

        private void BindGrid()
        {
            SqlQuery q = new Select().From<XMenu>();

            //if (btnNoHiddenMenu.Pressed)
            //{
            //    q.Where(XMenu.IsMainMenuColumn).IsEqualTo(true);
            //}

            q.OrderAsc(XMenu.SortIndexColumn.ColumnName);
            XMenuCollection menus = q.ExecuteAsCollection<XMenuCollection>();

            List<MyMenu> mys = XMenuHelper.GetMyMenuCollection(menus);
            Grid1.DataSource = mys;
            Grid1.DataBind();
        }

        #endregion

        #region Events

        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            int menuId = Convert.ToInt32(Grid1.DataKeys[e.RowIndex][0]);

            if (e.CommandName == "Delete")
            {
                XMenu.Delete(menuId);

                BindGrid();
            }
            //else if (e.CommandName == "Show")
            //{
            //    bool rowChecked = Convert.ToBoolean(Grid1.Rows[e.RowIndex].States[e.ColumnIndex]);

            //    XMenu menu = XMenu.FetchByID(menuId);
            //    menu.IsMainMenu = rowChecked;
            //    menu.Save(User.Identity.Name);

            //    BindGrid();
            //}
        }


        //protected void btnOnlyTwoLevelMenu_Click(object sender, EventArgs e)
        //{
        //    BindGrid();
        //}

        //protected void btnNoHiddenMenu_Click(object sender, EventArgs e)
        //{
        //    BindGrid();
        //}

        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion

    }
}
